//
//  EAWeatherForIP.m
//  WhatToEat
//
//  Created by Mac on 15/3/24.
//  Copyright (c) 2015年 Mac. All rights reserved.
//

#import "EAWeatherForIP.h"


@implementation EAWeatherForIP

static EAWeatherForIP * sharedSingleton = nil;

+ (EAWeatherForIP *) sharedEAWeatherForIP
{
    if (sharedSingleton == nil) {
        sharedSingleton = [[EAWeatherForIP alloc] init];
    }
    return sharedSingleton;
}




-(NSDictionary *)getWeatherForIP
{
    
    
    
    
    //解析网址通过ip 获取城市天气代码
    NSURL *url = [NSURL URLWithString:@"http://61.4.185.48:81/g/"];
    
    //    定义一个NSError对象，用于捕获错误信息
    NSError *error;
    
    
    
    NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
    //  NSString *resourceText = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.pcauto.com.cn/jingxuan/pic/1/"] encoding:enc error:NULL];
    
    
    
    
    NSString *jsonString = [NSString stringWithContentsOfURL:url encoding:enc error:&error];
    
    NSLog(@"------------%@",jsonString);
    
    // 得到城市代码字符串，截取出城市代码
    NSString *Str;
    for (int i = 0; i<=[jsonString length]; i++)
    {
        for (int j = i+1; j <=[jsonString length]; j++)
        {
            Str = [jsonString substringWithRange:NSMakeRange(i, j-i)];
            if ([Str isEqualToString:@"id"]) {
                if (![[jsonString substringWithRange:NSMakeRange(i+3, 1)] isEqualToString:@"c"]) {
                    _intString = [jsonString substringWithRange:NSMakeRange(i+3, 9)];
                    NSLog(@"***%@***",_intString);
                }
            }
        }
    }
    
    //中国天气网解析地址；
    NSString *path=[NSString stringWithFormat:@"http://m.weather.com.cn/atad/%@.html",_intString];
    
    NSLog(@"%@",path);
    if (_intString) {
        
        
        NSDictionary *responseObject=[self dataStringFromUrlString:path];
        
        
        
        if (responseObject) {
            NSDictionary *d=@{@"city": responseObject[@"weatherinfo"][@"city"],
                              @"time":responseObject[@"weatherinfo"][@"date_y"],
                              @"weather":responseObject[@"weatherinfo"][@"weather1"],
                              @"temp1":responseObject[@"weatherinfo"][@"temp1"],
                              @"wind1":responseObject[@"weatherinfo"][@"wind1"],
                              @"feel":responseObject[@"weatherinfo"][@"fx1"],
                              
                              
                              };
            
            
            
            [[NSUserDefaults standardUserDefaults] setObject:d forKey:[NSString stringWithFormat:@"%@+%@",[NSDate date],_intString]];
            [[NSUserDefaults standardUserDefaults] synchronize];
            
            return d;
        }else
        {
            NSDictionary *d=[[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:@"%@+%@",[NSDate date],_intString]];
            
            
            if (d) {
                return d;
            }
        
        
        
        }
 

        
    }else
    {
    
        NSDictionary *d=[[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:@"%@+%@",[NSDate date],_intString]];
        
        
        if (d) {
            return d;
        }

    
     
    
    }
    
    
    
    
    return @{};
    
    
    
    
    
}

-(NSDictionary *)dataStringFromUrlString:(NSString *)string
{
    NSURL *url = [NSURL URLWithString:string];
    
    //第二步，通过URL创建网络请求
    
    NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
    
    //第三步，连接服务器
    
    NSData *received = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    
    NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:received options:NSJSONReadingMutableLeaves error:nil];
    
    return resultDic;
}
@end
